<?php

namespace app\api\controller;

use app\api\model\PrintConfig;
use app\common\controller\Api;
use think\Model;
use think\Request;

/**
 * 首页接口
 */
class PrintUp extends Api
{
    protected $noNeedRight = ['*'];

    /**
     * 保存打印配置
    */
    public function saveConfig()
    {
        if(!Request::instance()->isPost()){
            $this->error('请求类型错误');
        }
        $userInfo = $this->auth->getUserinfo();
        $userId = $userInfo['id'];
        $textDirection = $this->request->post('text_direction');
        if(empty($textDirection)){
            $this->error('请选选择文字方向');
        }
        $rotation = $this->request->post('rotation');
        if(empty($rotation)){
            $this->error('请选选择旋转角度');
        }
        $pageWidth = $this->request->post('page_width');
        if(empty($pageWidth)){
            $this->error('请输页面宽度');
        }
        $bodyWidth = $this->request->post('body_width');
        if(empty($bodyWidth)){
            $this->error('请输界面宽度');
        }
        $leftMargin = $this->request->post('left_margin');
        if(empty($leftMargin)){
            $this->error('请输左边距');
        }
        $rightMargin = $this->request->post('right_margin');
        if(empty($rightMargin)){
            $this->error('请输右边距');
        }
        $topMargin = $this->request->post('top_margin');
        if(empty($topMargin)){
            $this->error('请输上边距');
        }
        $bottomMargin = $this->request->post('bottom_margin');
        if(empty($bottomMargin)){
            $this->error('请输入下边距');
        }
        $lines = $this->request->post('lines');
        if(empty($lines)){
            $this->error('请输入行数');
        }
        $speed = $this->request->post('speed');
        if(empty($speed)){
            $this->error('请选选择打印速度');
        }
        $config = [
            'text_direction' => $textDirection,
            'rotation' => $rotation,
            'page_width' => $pageWidth,
            'body_width' => $bodyWidth,
            'left_margin' => $leftMargin,
            'right_margin' => $rightMargin,
            'top_margin' => $topMargin,
            'bottom_margin' => $bottomMargin,
            'lines' => $lines,
            'speed' => $speed,
        ];
        $this->_saveConfig($userId,$config);
        $this->success('保存成功');
    }

    /**
     * 打印配置
     *
     */
    public function getConfig()
    {
        $userInfo = $this->auth->getUserinfo();
        $printConfig = model('PrintConfig')->where('user_id','=',$userInfo['id'])->find();
        if(!$printConfig){
            $printConfig = [];
        }
        $this->success('打印配置',$printConfig);
    }

    /**
     * 发送打印
    */
    public function send()
    {
        if(!Request::instance()->isPost()){
            $this->error('请求类型错误');
        }
        $userInfo = $this->auth->getUserinfo();
        $userId = $userInfo['id'];
        $attachmentId = $this->request->post('attachment_id');
        if(empty($attachmentId)){
            $this->error('请选择打印字体');
        }
        $textDirection = $this->request->post('text_direction');
        if(empty($textDirection)){
            $this->error('请选选择文字方向');
        }
        $rotation = $this->request->post('rotation');
        if(empty($rotation)){
            $this->error('请选选择旋转角度');
        }
        $pageWidth = $this->request->post('page_width');
        if(empty($pageWidth)){
            $this->error('请输页面宽度');
        }
        $bodyWidth = $this->request->post('body_width');
        if(empty($bodyWidth)){
            $this->error('请输界面宽度');
        }
        $leftMargin = $this->request->post('left_margin');
        if(empty($leftMargin)){
            $this->error('请输左边距');
        }
        $rightMargin = $this->request->post('right_margin');
        if(empty($rightMargin)){
            $this->error('请输右边距');
        }
        $topMargin = $this->request->post('top_margin');
        if(empty($topMargin)){
            $this->error('请输上边距');
        }
        $bottomMargin = $this->request->post('bottom_margin');
        if(empty($bottomMargin)){
            $this->error('请输入下边距');
        }
        $lines = $this->request->post('lines');
        if(empty($lines)){
            $this->error('请输入行数');
        }
        $speed = $this->request->post('speed');
        if(empty($speed)){
            $this->error('请选选择打印速度');
        }
        $config = [
            'text_direction' => $textDirection,
            'rotation' => $rotation,
            'page_width' => $pageWidth,
            'body_width' => $bodyWidth,
            'left_margin' => $leftMargin,
            'right_margin' => $rightMargin,
            'top_margin' => $topMargin,
            'bottom_margin' => $bottomMargin,
            'lines' => $lines,
            'speed' => $speed,
        ];
        $this->_saveConfig($userId,$config);
        $this->success('打印成功');

    }

    /**
     * 存储打印配置
    */
    private function _saveConfig($userId,$config)
    {
        $printConfig = model('PrintConfig')->where('user_id','=',$userId)->find();
        if(!$printConfig){
            $printConfig = new PrintConfig();
        }
        $printConfig->user_id = $userId;
        $printConfig->text_direction = $config['text_direction'];
        $printConfig->rotation = $config['rotation'];
        $printConfig->page_width = $config['page_width'];
        $printConfig->body_width = $config['body_width'];
        $printConfig->left_margin = $config['left_margin'];
        $printConfig->right_margin = $config['right_margin'];
        $printConfig->top_margin = $config['top_margin'];
        $printConfig->bottom_margin = $config['bottom_margin'];
        $printConfig->lines = $config['lines'];
        $printConfig->speed = $config['speed'];
        $printConfig->save();
    }
}
